package com.dachuang.canjiren.service.impl;

import com.dachuang.canjiren.entity.Banner;
import com.dachuang.canjiren.entity.Policy;
import com.dachuang.canjiren.mapper.BannerMapper;
import com.dachuang.canjiren.mapper.PolicyMapper;
import com.dachuang.canjiren.service.PolicyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dachuang.common.exception.BizCodeEnume;
import com.dachuang.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-27
 */
@Service
public class PolicyServiceImpl implements PolicyService {

    @Autowired
    private PolicyMapper policyMapper;

    @Override
    public R insertPolicy(String theme, String content) {
        if (StringUtils.isEmpty(theme)) {
            return R.error(BizCodeEnume.ERROR30001);
        }
        if (StringUtils.isEmpty(content)) {
            return R.error(BizCodeEnume.ERROR30002);
        }
        Policy policy = new Policy();
        policy.setTheme(theme);
        policy.setContent(content);
        int i = policyMapper.insertPolicy(policy);
        if (i > 0) {
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R selectPolicy(Integer id) {
        if (Objects.isNull(id)){
            return R.error(BizCodeEnume.ERROR20005);
        }
        Policy policy = policyMapper.selectPolicy(id);
        if (Objects.isNull(policy)){
            return R.error(BizCodeEnume.ERROR30003);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("data", policy);
        return R.ok(map);
    }

}

